//
//  MapViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "MapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

#define APIKey      @"573f2a20a6f35e0ca19a27b163cae589"

@interface MapViewController ()<MAMapViewDelegate, AMapSearchDelegate, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *annotations;

@property (nonatomic, retain) NSArray *array;

@property (nonatomic, retain) NSMutableArray *poiArray;

@property (nonatomic, retain) MAMapView *mapView;

//路况
@property (nonatomic, retain) UIButton *road;
//当前位置
@property (nonatomic, retain) UIButton *currentArea;

@property (nonatomic, retain) AMapSearchAPI *search;

//周边餐饮
@property (nonatomic, retain) UIButton *restaurant;
//周边宾馆
@property (nonatomic, retain) UIButton *hotel;
//周边景点
@property (nonatomic, retain) UIButton *attractions;
//周边公交站
@property (nonatomic, retain) UIButton *busStops;

@property (nonatomic, retain) UIView *searchView;

@property (nonatomic, retain) UIButton *turnSearch;

@property (nonatomic, retain) UIButton *pathButton;

@property (nonatomic, retain) UIButton *turnButton;

@property (nonatomic, retain) MAPointAnnotation *destinationPoint;

@property (nonatomic, retain) MAPointAnnotation *annotation;

@property (nonatomic, retain) CLLocation *currentLocation;

@property (nonatomic, assign) CLLocationCoordinate2D searchLocation;

@property (nonatomic, retain) AMapReGeocodeSearchRequest *currentRequest;

@property (nonatomic, retain) NSArray *pathPolyArray;
@end
@implementation MapViewController

- (void)dealloc
{
    [_AreaName release];
    [_tableView release];
    [_annotations release];
    [_array release];
    [_poiArray release];
    [_mapView release];
    
    [_road release];
    [_currentArea release];
    [_search release];
    
    [_restaurant release];
    [_hotel release];
    [_attractions release];
    [_busStops release];
    [_searchView release];
    [_turnSearch release];
    [_pathButton release];
    [_turnButton release];
    [_destinationPoint release];
    [_annotation release];
    [_currentLocation release];
    [_currentRequest release];
    [_pathPolyArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    

    //测试数据


//    _Wlatitude = 26.876801;
//    _Wlongitude = 100.225998;
   
    
    [self initMapView];
    
    //返回按钮
    [self initTurnButton];
    
    [self initRoad];
    
    [self initCurrentArea];
    
    [self initSearch];
    //(反地理编码)
    [self reGeoAction];
    
    //[self initTableView];
 
    //搜索按钮
    [self initSearchButton];
    //初始化餐厅酒店景点和公交站点
    [self initSearchView];
    //路线规划
    [self initPathButton];
    
    [self initAttributes];
    
}
#pragma mark init
- (void)initTurnButton{
 
    self.turnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.turnButton setImage:[UIImage imageNamed:@"Food_back"] forState:UIControlStateNormal];
    self.turnButton.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    [self.turnButton addTarget:self action:@selector(turnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.turnButton.frame = CGRectMake(10 *kWidth, 25 *kHeight, 40 *kWidth, 40 *kWidth);
    self.turnButton.layer.cornerRadius = 20 *kWidth;
    [self.view addSubview:_turnButton];
}
//创建地图
- (void)initMapView{
    [MAMapServices sharedServices].apiKey = APIKey;
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, -20 *kHeight, 375 *kWidth, self.view.frame.size.height + 20 *kHeight)];
    _mapView.delegate = self;
    _mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, 42 *kHeight);
    _mapView.scaleOrigin = CGPointMake(_mapView.scaleOrigin.x, 42 *kHeight);
    
    [self.view addSubview:_mapView];
    
    _mapView.showsUserLocation = YES;
    
    [_mapView release];

}
- (void)initAttributes
{
    self.annotations = [NSMutableArray array];
    self.array = [NSArray array];
    self.poiArray = [NSMutableArray array];
}
- (void)initTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 + 80 *kHeight, 375 *kWidth, self.view.frame.size.height / 2 - 80 * kHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
}
//路况
- (void)initRoad{

    self.road = [UIButton buttonWithType:UIButtonTypeCustom];
    self.road.frame = CGRectMake(320 *kWidth, 90 *kHeight, 50 *kWidth, 50 *kHeight);
    [self.road setImage:[UIImage imageNamed:@"lukuang"] forState:UIControlStateNormal];
    self.road.backgroundColor = [UIColor whiteColor];
    [self.road addTarget:self action:@selector(loadAction:) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:self.road];

}
//当前位置
- (void)initCurrentArea{

    self.currentArea = [UIButton buttonWithType:UIButtonTypeCustom];
    self.currentArea.frame = CGRectMake(320 *kWidth, 150 *kHeight, 50 *kWidth, 50 *kHeight);
    [self.currentArea setImage:[UIImage imageNamed:@"Enter_Location"] forState:UIControlStateNormal];
    self.currentArea.backgroundColor = [UIColor whiteColor];
    [self.currentArea addTarget:self action:@selector(currentAreaAction:) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:_currentArea];
}
//初始化搜索
- (void)initSearch{
    
    self.search = [[AMapSearchAPI alloc] initWithSearchKey:APIKey Delegate:self];
    [_search release];

}
//搜索按钮
- (void)initSearchButton{

    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    searchButton.frame = CGRectMake(320 *kWidth, 210 *kHeight , 50 *kWidth, 50 *kHeight);
    searchButton.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
    searchButton.backgroundColor = [UIColor whiteColor];
    [searchButton setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
    [_mapView addSubview:searchButton];

}
- (void)initSearchView{
    self.searchView = [[UIView alloc] initWithFrame:CGRectMake(0, 1000 *kHeight, 375 *kWidth, 250 *kHeight)];
    self.searchView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
    [self.view addSubview:_searchView];
    
    self.restaurant = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.restaurant setImage:[UIImage imageNamed:@"MAMapView_canting"] forState:UIControlStateNormal];
    self.restaurant.frame = CGRectMake(25 *kWidth, 20 *kHeight, 50 *kWidth, 50 *kWidth);
    [self.restaurant addTarget:self action:@selector(restaurantAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:_restaurant];
    
    UILabel *restaurantLabel = [[UILabel alloc] initWithFrame:CGRectMake(25 *kWidth, 75 *kHeight, 50 *kWidth, 20 *kWidth)];
    restaurantLabel.text = @"美食";
    restaurantLabel.textAlignment = NSTextAlignmentCenter;
    [self.searchView addSubview:restaurantLabel];
    [restaurantLabel release];
    
    self.hotel = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.hotel setImage:[UIImage imageNamed:@"MAMapView_jiudian"] forState:UIControlStateNormal];

    self.hotel.frame = CGRectMake(115 *kWidth, 20 *kHeight, 50 *kWidth, 50 *kWidth);
    [self.hotel addTarget:self action:@selector(hotelAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:_hotel];
    
    UILabel *hotelLabel = [[UILabel alloc] initWithFrame:CGRectMake(115 *kWidth, 75 *kHeight, 50 *kWidth, 20 *kWidth)];
    hotelLabel.text = @"酒店";
    hotelLabel.textAlignment = NSTextAlignmentCenter;
    [self.searchView addSubview:hotelLabel];
    [hotelLabel release];
    
    self.attractions = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.attractions setImage:[UIImage imageNamed:@"MAMapView_jingdian"] forState:UIControlStateNormal];
    self.attractions.frame = CGRectMake(205 *kWidth, 20 *kHeight, 50 *kWidth, 50 *kWidth);
    [self.attractions addTarget:self action:@selector(attractionsAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:_attractions];
    
    UILabel *attractionsLabel = [[UILabel alloc] initWithFrame:CGRectMake(205 *kWidth, 75 *kHeight, 50 *kWidth, 20 *kWidth)];
    attractionsLabel.text = @"景点";
    attractionsLabel.textAlignment = NSTextAlignmentCenter;
    [self.searchView addSubview:attractionsLabel];
    [attractionsLabel release];
    
    self.busStops = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.busStops setImage:[UIImage imageNamed:@"MAMapView_jiayouzhan"] forState:UIControlStateNormal];
    self.busStops.frame = CGRectMake(295 *kWidth, 20 *kHeight, 50 *kWidth, 50 *kWidth);
    [self.busStops addTarget:self action:@selector(busStopsAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:_busStops];
    
    UILabel *busStopsLabel = [[UILabel alloc] initWithFrame:CGRectMake(280 *kWidth, 75 *kHeight, 80 *kWidth, 20 *kWidth)];
    busStopsLabel.text = @"加油站";
    busStopsLabel.textAlignment = NSTextAlignmentCenter;
    [self.searchView addSubview:busStopsLabel];
    [busStopsLabel release];
    
    self.turnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.turnSearch setImage:[UIImage imageNamed:@"MAMapView_quxiao"] forState:UIControlStateNormal];
    self.turnSearch.frame = CGRectMake(self.view.frame.size.width / 2 - 25 *kWidth, 130 *kHeight, 50 *kWidth, 50 *kWidth);
    [self.turnSearch addTarget:self action:@selector(turnSearchAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.searchView addSubview:_turnSearch];
   
    UILabel *turnSearchLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 25 *kWidth, 200 *kHeight, 50 *kWidth, 20 *kWidth)];
    turnSearchLabel.text = @"取 消";
    turnSearchLabel.textAlignment = NSTextAlignmentCenter;
    [self.searchView addSubview:turnSearchLabel];
    [turnSearchLabel release];
    
    [_searchView release];
}
- (void)initPathButton{
    self.pathButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pathButton.frame = CGRectMake(320 *kWidth, 270 *kHeight, 50 *kWidth, 50 *kHeight);
    [self.pathButton setImage:[UIImage imageNamed:@"luxianguihua"] forState:UIControlStateNormal];
    self.pathButton .backgroundColor = [UIColor whiteColor];
    [self.pathButton addTarget:self action:@selector(pathButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_pathButton];
}
#pragma mark Action
//路况的Action
- (void)loadAction:(UIButton *)road{

    road.selected = !road.isSelected;
    if (road.selected) {
        [self.road setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        self.mapView.showTraffic = YES;
    }else{
        self.mapView.showTraffic = NO;
        [self.road setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }
}
//当前位置的Action
- (void)currentAreaAction:(UIButton *)currentArea{

    self.mapView.userTrackingMode = MAUserTrackingModeFollowWithHeading;
    
    if (_currentLocation) {
        //secrch请求用到的类 AMapReGeoCodeSearchRequest
        self.currentRequest = [[AMapReGeocodeSearchRequest alloc] init];
        self.currentRequest.location = [AMapGeoPoint locationWithLatitude:_currentLocation.coordinate.latitude longitude:_currentLocation.coordinate.longitude];
        
        [_currentRequest release];
    }
}
//进行逆地理请求
- (void)reGeoAction{

    AMapReGeocodeSearchRequest *request = [[AMapReGeocodeSearchRequest alloc] init];
    request.location = [AMapGeoPoint locationWithLatitude:_Wlatitude longitude:_Wlongitude];
     _mapView.centerCoordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
    //逆地理编码请求
    [_search AMapReGoecodeSearch:request];
    
    [request release];
}
- (void)searchAction
{
    [self.tableView removeFromSuperview];
    [UIView animateWithDuration:0.5 animations:^{
        self.searchView.frame = CGRectMake(0, 420 *kHeight, 375 *kWidth, 250 * kHeight);
    }];
    
}
- (void)search:(NSString *)str{

        if ( _search == nil)
        {
            NSLog(@"search failed");
            return;
        }
    
        AMapPlaceSearchRequest *request = [[AMapPlaceSearchRequest alloc] init];
        request.searchType = AMapSearchType_PlaceAround;
        request.location = [AMapGeoPoint locationWithLatitude:_Wlatitude longitude:_Wlongitude];
        request.radius = 10000;
        request.keywords = str;
    
    [_search AMapPlaceSearch:request];
    
    [request release];
}
- (void)restaurantAction:(UIButton *)btn{

    [self turnSearchAction:self.turnSearch];
    
    [self search:@"餐厅"];

}
- (void)hotelAction:(UIButton *)btn{

    [self turnSearchAction:self.turnSearch];
    
    [self search:@"酒店"];

}
- (void)attractionsAction:(UIButton *)btn{

    
    [self turnSearchAction:self.turnSearch];
    
    [self search:@"景点"];

}

- (void)busStopsAction:(UIButton *)btn{

    [self turnSearchAction:self.turnSearch];
    
    [self search:@"加油站"];

}
- (void)turnSearchAction:(UIButton *)btn{

    [UIView animateWithDuration:1 animations:^{
        self.searchView.frame = CGRectMake(0, 1000 *kHeight, 375 *kHeight, 250 *kHeight);
    }];

    
}

- (void)pathButtonAction:(UIButton *)btn{

    if (_search == nil) {
        return;
    }
    AMapNavigationSearchRequest *request = [[AMapNavigationSearchRequest alloc] init];
    
    // 设置为步行路径规划
    request.searchType = AMapSearchType_NaviDrive;
    
    request.origin = [AMapGeoPoint locationWithLatitude:_Wlatitude longitude:_Wlongitude];
    request.destination = [AMapGeoPoint locationWithLatitude:_searchLocation.latitude longitude:_searchLocation.longitude];
    
    [_search AMapNavigationSearch:request];
    
    
    [request release];

}
- (CLLocationCoordinate2D *)coordinatesForString:(NSString *)string
                                 coordinateCount:(NSUInteger *)coordinateCount
                                      parseToken:(NSString *)token
{
    if (string == nil)
    {
        return NULL;
    }
    
    if (token == nil)
    {
        token = @",";
    }
    
    NSString *str = @"";
    if (![token isEqualToString:@","])
    {
        str = [string stringByReplacingOccurrencesOfString:token withString:@","];
    }
    
    else
    {
        str = [NSString stringWithString:string];
    }
    
    NSArray *components = [str componentsSeparatedByString:@","];
    NSUInteger count = [components count] / 2;
    if (coordinateCount != NULL)
    {
        *coordinateCount = count;
    }
    CLLocationCoordinate2D *coordinates = (CLLocationCoordinate2D*)malloc(count * sizeof(CLLocationCoordinate2D));
    
    for (int i = 0; i < count; i++)
    {
        coordinates[i].longitude = [[components objectAtIndex:2 * i]     doubleValue];
        coordinates[i].latitude  = [[components objectAtIndex:2 * i + 1] doubleValue];
    }
    
    return coordinates;
}

- (NSArray *)polylinesForPath:(AMapPath *)path
{
    if (path == nil || path.steps.count == 0)
    {
        return nil;
    }
    
    NSMutableArray *polylines = [NSMutableArray array];
    
    [path.steps enumerateObjectsUsingBlock:^(AMapStep *step, NSUInteger idx, BOOL *stop) {
        
        NSUInteger count = 0;
        CLLocationCoordinate2D *coordinates = [self coordinatesForString:step.polyline
                                                         coordinateCount:&count
                                                              parseToken:@";"];
        
        MAPolyline *polyline = [MAPolyline polylineWithCoordinates:coordinates count:count];
        [polylines addObject:polyline];
        
        free(coordinates), coordinates = NULL;
    }];
    
    return polylines;
}
- (void)turnButtonAction:(UIButton *)btn{

    [self.navigationController popViewControllerAnimated:YES];

}
#pragma mark mapView的代理回调方法
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay{
    
    if ([overlay isKindOfClass:[MAPolyline class]]) {
        MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];
        polylineView.lineWidth = 4;
        polylineView.strokeColor = [UIColor purpleColor];
        return [polylineView autorelease];
    }
    return nil;
    
}
//位置或者设备方向更新后，会调用此函数。
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
        
        //将用户当前坐标写成属性（以后会用）
        _currentLocation = userLocation.location;
        
}
#pragma mark Search回调方法
- (void)searchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"request :%@, error :%@", request, error);
}
- (void)onNavigationSearchDone:(AMapNavigationSearchRequest *)request response:(AMapNavigationSearchResponse *)response{
    
    if (response.count > 0) {
        [_mapView removeOverlays:self.pathPolyArray];
        self.pathPolyArray = nil;
        
        //只显示第一条方案
        self.pathPolyArray = [self polylinesForPath:response.route.paths[0]];
    
        [_mapView addOverlays:self.pathPolyArray];
        
        [_mapView showAnnotations:@[_annotation, _destinationPoint] animated:YES];
    }
    
}
//逆地理编码回调
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response{
    
    //请求的结果 都在"response.regeocode"中
    
    if (request == _currentRequest) {
        NSString *title = response.regeocode.addressComponent.city;
        if (title.length == 0) {
            //title的长度为0表示此地为直辖市（省）
            title = response.regeocode.addressComponent.province;
        }
        _mapView.userLocation.title = title;
        _mapView.userLocation.subtitle = response.regeocode.formattedAddress;
    }
    else{
    
        // 添加标注
        if (_destinationPoint != nil)
        {
            // 清理
            [_mapView removeAnnotation:_destinationPoint];
            _destinationPoint = nil;
            
        }
        self.destinationPoint = [[MAPointAnnotation alloc] init];
        _destinationPoint.coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
        _destinationPoint.title = response.regeocode.formattedAddress;
        
        [_mapView addAnnotation:_destinationPoint];
        
        [_destinationPoint release];
    }
    
}
- (void)onPlaceSearchDone:(AMapPlaceSearchRequest *)request response:(AMapPlaceSearchResponse *)response
{
    if (response.pois.count > 0)
    {
        self.array = response.pois;
        
        //self.mapView.frame = CGRectMake(0, -20 *kHeight, 375 *kWidth, self.view.frame.size.height / 2 + 100 *kHeight);
        
        [self initTableView];

        //[self.tableView reloadData];
        
        // 清空标注
        [_mapView removeAnnotations:_annotations];
        [_annotations removeAllObjects];
        [_poiArray removeAllObjects];
    }
    
}

//地理编码回调
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response{
    AMapGeocode *geocode = response.geocodes.firstObject;
    _mapView.centerCoordinate = CLLocationCoordinate2DMake(geocode.location.latitude, geocode.location.longitude);
    //判断返回结果是否为空
    if (response.count == 0) {
        return;
    }
    // 添加标注
    if (_destinationPoint != nil)
    {
        // 清理
        [_mapView removeAnnotation:_destinationPoint];
        _destinationPoint = nil;
       
    }
    self.destinationPoint = [[MAPointAnnotation alloc] init];
    _destinationPoint.coordinate = CLLocationCoordinate2DMake(geocode.location.latitude, geocode.location.longitude);;
    _destinationPoint.title = geocode.formattedAddress;
    
    [_mapView addAnnotation:_destinationPoint];
    
    [_destinationPoint release];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if (annotation == _destinationPoint)
    {
        static NSString *reuseIndetifier = @"startAnnotationReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier] autorelease];
        }
        
        annotationView.canShowCallout = YES;
        annotationView.animatesDrop = YES;
        
        return annotationView;
    }
    
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAPinAnnotationView *annotationView = (MAPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier] autorelease];
        }
        
        annotationView.canShowCallout = YES;
        annotationView.animatesDrop = YES;
        
        return annotationView ;
    }
    
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",[self.array class]);
    
    static NSString *cellIdentifier = @"tablecellIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier] autorelease];
    }
    
    AMapPOI *poi = self.array[indexPath.row];
    
    cell.textLabel.text = poi.name;
    cell.detailTextLabel.text = poi.address;
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [_mapView removeAnnotations:_annotations];
    [_annotations removeAllObjects];
    [_poiArray removeAllObjects];
    
    // 为点击的poi点添加标注
    AMapPOI *poi = _array[indexPath.row];
    if ([_poiArray containsObject:[NSNumber numberWithInteger:indexPath.row]]) {
        NSLog(@"存在了");
        _mapView.centerCoordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
    }
    else{
        NSLog(@"没有");

    self.annotation = [[MAPointAnnotation alloc] init];
    _annotation.coordinate = CLLocationCoordinate2DMake(poi.location.latitude, poi.location.longitude);
    _annotation.title = poi.name;
    _annotation.subtitle = poi.address;
    
    //将其移动到中心点
    _mapView.centerCoordinate = _annotation.coordinate;
        
    _searchLocation = _annotation.coordinate;
        
    [_mapView addAnnotation:_annotation];
    [_annotations addObject:_annotation];
    
    [_poiArray addObject:[NSNumber numberWithInteger:indexPath.row]];
    
    [_annotation release];
        
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.tableView removeFromSuperview];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
